home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / corelib / ncbimain.vms < prev    next >
Text File  |  1996-07-05  |  9KB  |  312 lines

  1. /*   ncbimain.c
  2. * ===========================================================================
  3. *
  4. *                            PUBLIC DOMAIN NOTICE                          
  5. *               National Center for Biotechnology Information
  6. *                                                                          
  7. *  This software/database is a "United States Government Work" under the   
  8. *  terms of the United States Copyright Act.  It was written as part of    
  9. *  the author's official duties as a United States Government employee and 
  10. *  thus cannot be copyrighted.  This software/database is freely available 
  11. *  to the public for use. The National Library of Medicine and the U.S.    
  12. *  Government have not placed any restriction on its use or reproduction.  
  13. *                                                                          
  14. *  Although all reasonable efforts have been taken to ensure the accuracy  
  15. *  and reliability of the software and data, the NLM and the U.S.          
  16. *  Government do not and cannot warrant the performance or results that    
  17. *  may be obtained by using this software or data. The NLM and the U.S.    
  18. *  Government disclaim all warranties, express or implied, including       
  19. *  warranties of performance, merchantability or fitness for any particular
  20. *  purpose.                                                                
  21. *                                                                          
  22. *  Please cite the author in any work or product based on this material.   
  23. *
  24. * ===========================================================================
  25. *
  26. * File Name:  ncbimain.c
  27. *
  28. * Author:  Ostell
  29. *
  30. * Version Creation Date:   2/25/91
  31. *
  32. * Version Number:  1.1
  33. *
  34. * File Description: 
  35. *       portable main() and argc, argv handling
  36. *       VMS version
  37. *
  38. * Modifications:  
  39. * --------------------------------------------------------------------------
  40. * Date     Name        Description of modification
  41. * -------  ----------  -----------------------------------------------------
  42. * 2/25/91  Ostell      add support to GetArgs for args separated from tags
  43. *
  44. * ==========================================================================
  45. */
  46.  
  47. #include <ncbi.h>
  48. #include <ncbiwin.h>
  49.  
  50. /*****************************************************************************
  51. *
  52. *   main()
  53. *     this replaces the normal program main()
  54. *     handles argc and argv
  55. *     does any required windows initialization
  56. *
  57. *****************************************************************************/
  58. static int targc;
  59. static char ** targv;
  60.  
  61. main (int argc, char *argv[])
  62.  
  63. {
  64.     Nlm_Int2 retval;
  65.  
  66.     targc = argc;
  67.     targv = argv;
  68.                        /**** do any initialization here ******/
  69.     retval = Nlm_Main();
  70.                        /**** do any cleanup here *************/
  71.     Nlm_FreeConfigStruct ();
  72.     exit(retval);
  73. }
  74. #define MAX_ARGS 50       /* maximum args this can process */
  75. /*****************************************************************************
  76. *
  77. *   Nlm_GetArgs(ap)
  78. *       returns user startup arguments
  79. *
  80. *****************************************************************************/
  81. Nlm_Boolean Nlm_GetArgs (Nlm_CharPtr progname, Nlm_Int2 numargs, Nlm_ArgPtr ap)
  82.  
  83. {
  84.     static Nlm_CharPtr types[9] = {
  85.         NULL,
  86.         "T/F",
  87.         "Integer",
  88.         "Real",
  89.         "String",
  90.         "File In",
  91.         "File Out",
  92.         "Data In",
  93.         "Data Out" };
  94.     Nlm_Boolean okay = FALSE, all_default = TRUE, range;
  95.     Nlm_Int2 i, j;
  96.     Nlm_Int4 ifrom, ito;
  97.     Nlm_FloatHi ffrom, fto;
  98.     Nlm_ArgPtr curarg;
  99.     Nlm_Boolean resolved[MAX_ARGS];
  100.     Nlm_CharPtr arg;
  101.     Nlm_Char tmp;
  102.     Nlm_Boolean ok;
  103.  
  104.     if ((ap == NULL) || (numargs == 0) || (numargs > MAX_ARGS))
  105.         return okay;
  106.  
  107.     curarg = ap;                        /* set defaults */
  108.     Nlm_MemFill(resolved, '\0', (MAX_ARGS * sizeof(Nlm_Boolean)));
  109.  
  110.     for (i = 0; i < numargs; i++, curarg++)
  111.     {
  112.         if ((curarg->type < ARG_BOOLEAN) ||
  113.              (curarg->type > ARG_DATA_OUT))
  114.         {
  115.             Message(MSG_ERROR, "Invalid Arg->type in %s", curarg->prompt);
  116.             return okay;
  117.         }
  118.         curarg->intvalue = 0;
  119.         curarg->floatvalue = 0.0;
  120.         curarg->strvalue = NULL;
  121.         if (curarg->defaultvalue != NULL)
  122.         {
  123.             resolved[i] = TRUE;
  124.             switch (curarg->type)
  125.             {
  126.                 case ARG_BOOLEAN:
  127.                     if (TO_UPPER(*curarg->defaultvalue) == 'T')
  128.                         curarg->intvalue = 1;
  129.                     else
  130.                         curarg->intvalue = 0;
  131.                     break;
  132.                 case ARG_INT:
  133.                     sscanf(curarg->defaultvalue, "%ld", &curarg->intvalue);
  134.                     break;
  135.                 case ARG_FLOAT:
  136.                     sscanf(curarg->defaultvalue, "%lf", &curarg->floatvalue);
  137.                     break;
  138.                 case ARG_STRING:
  139.                 case ARG_FILE_IN:
  140.                 case ARG_FILE_OUT:
  141.                 case ARG_DATA_IN:
  142.                 case ARG_DATA_OUT:
  143.                     curarg->strvalue = StringSave (curarg->defaultvalue);
  144.                     break;
  145.             }
  146.         }
  147.         else if (curarg->optional == FALSE)
  148.             all_default = FALSE;    /* must have some arguments */
  149.     }
  150.                           /**** show usage if no args on command line ***/
  151.     if ((targc == 1) && (all_default == TRUE))   /* no args ok */
  152.         return TRUE;
  153.  
  154.     if ((targc == 1) || (*(targv[1]+1) == '\0'))
  155.     {
  156.         printf("\n%s   arguments:\n\n", progname);
  157.         curarg = ap;
  158.  
  159.         for (i = 0, j=0; i < numargs; i++, j++, curarg++)
  160.         {
  161.             printf("  -%c  %s [%s]", curarg->tag, curarg->prompt,
  162.                 types[curarg->type]);
  163.             if (curarg->optional)
  164.                 printf("  Optional");
  165.             printf("\n");
  166.             if (curarg->defaultvalue != NULL)
  167.                 printf("    default = %s\n", curarg->defaultvalue);
  168.             if ((curarg->from != NULL) || (curarg->to != NULL))
  169.             {
  170.                 if ((curarg->type == ARG_DATA_IN) ||
  171.                     (curarg->type == ARG_DATA_OUT))
  172.                     printf("    Data Type = %s\n", curarg->from);
  173.                 else
  174.                     printf("    range from %s to %s\n", curarg->from, curarg->to);
  175.             }
  176.         }
  177.         printf("\n");
  178.         return okay;
  179.     }
  180.  
  181.     for (i = 1; i < targc; i++)
  182.     {
  183.         arg = targv[i];
  184.         if (*arg != '-')
  185.         {
  186.             Message(MSG_ERROR, "Arguments must start with -");
  187.             return okay;
  188.         }
  189.         arg++;
  190.         curarg = ap;
  191.         for (j = 0; j < numargs; j++, curarg++)
  192.         {
  193.             if (*arg == curarg->tag)
  194.                 break;
  195.         }
  196.         if (j == numargs)
  197.         {
  198.             Message(MSG_ERROR, "Invalid argument: %s", targv[i]);
  199.             return okay;
  200.         }
  201.         arg++;
  202.         if (*arg == '\0')
  203.         {
  204.             ok = FALSE;
  205.             if ((i + 1) < targc)  /* argument comes after space */
  206.             {
  207.                 if (*targv[i + 1] == '-')
  208.                 {
  209.                     tmp = *(targv[i+1]+1);
  210.                     if (((curarg->type == ARG_INT) || (curarg->type == ARG_FLOAT)) &&
  211.                         ((tmp == '.') || (IS_DIGIT(tmp))))
  212.                         ok = TRUE;
  213.                 }
  214.                 else
  215.                     ok = TRUE;
  216.                 if (ok)
  217.                 {
  218.                     i++;
  219.                     arg = targv[i];
  220.                 }
  221.             }
  222.  
  223.             if ((!ok) && (curarg->type != ARG_BOOLEAN))
  224.             {
  225.                 Message(MSG_ERROR, "No argument given for %s", curarg->prompt);
  226.                 return okay;
  227.             }
  228.         }
  229.         resolved[j] = TRUE;
  230.         switch (curarg->type)
  231.         {
  232.             case ARG_BOOLEAN:
  233.                 if (TO_UPPER(*arg) == 'T')
  234.                     curarg->intvalue = 1;
  235.                 else if (TO_UPPER(*arg) == 'F')
  236.                     curarg->intvalue = 0;
  237.                 else if (*arg == '\0')
  238.                     curarg->intvalue = 1;
  239.                 break;
  240.             case ARG_INT:
  241.                 sscanf(arg, "%ld", &curarg->intvalue);
  242.                 if ((curarg->from != NULL) || (curarg->to != NULL))
  243.                 {
  244.                     range = TRUE;
  245.                     if (curarg->from != NULL)
  246.                     {
  247.                         sscanf(curarg->from, "%ld", &ifrom);
  248.                         if (curarg->intvalue < ifrom)
  249.                             range = FALSE;
  250.                     }
  251.                     if (curarg->to != NULL)
  252.                     {              
  253.                         sscanf(curarg->to, "%ld", &ito);
  254.                         if (curarg->intvalue > ito)
  255.                             range = FALSE;
  256.                     }
  257.                     if (! range)
  258.                     {
  259.                         Message(MSG_ERROR, "%s [%ld] is out of range [%s to %s]", curarg->prompt,
  260.                             curarg->intvalue, curarg->from, curarg->to);
  261.                         return okay;
  262.                     }
  263.                 }
  264.                  break;
  265.             case ARG_FLOAT:
  266.                 sscanf(arg, "%lf", &curarg->floatvalue);
  267.                 if ((curarg->from != NULL) || (curarg->to != NULL))
  268.                 {
  269.                     range = TRUE;
  270.                     if (curarg->from != NULL)
  271.                     {
  272.                         sscanf(curarg->from, "%lf", &ffrom);
  273.                         if (curarg->floatvalue < ffrom)
  274.                             range = FALSE;
  275.                     }
  276.                     if (curarg->to != NULL)
  277.                     {
  278.                         sscanf(curarg->to, "%lf", &fto);
  279.                         if (curarg->floatvalue > fto)
  280.                             range = FALSE;
  281.                     }
  282.                     if (! range)
  283.                     {
  284.                         Message(MSG_ERROR, "%s [%g] is out of range [%s to %s]", curarg->prompt, 
  285.                             curarg->floatvalue, curarg->from, curarg->to);
  286.                         return okay;
  287.                     }
  288.                 }
  289.                 break;
  290.             case ARG_STRING:
  291.             case ARG_FILE_IN:
  292.             case ARG_FILE_OUT:
  293.             case ARG_DATA_IN:
  294.             case ARG_DATA_OUT:
  295.                 curarg->strvalue = StringSave (arg);
  296.                 break;
  297.         }    /*** end switch ****/
  298.     }       
  299.     curarg = ap;
  300.     okay = TRUE;
  301.     for (i = 0; i < numargs; i++, curarg++)
  302.     {
  303.         if ((! curarg->optional) && (! resolved[i]))
  304.         {
  305.             Message(MSG_ERROR, "%s was not given an argument", curarg->prompt);
  306.             okay = FALSE;
  307.         }
  308.     }
  309.     return okay;
  310. }
  311.  
  312.